package com.metaera.app.utils;

import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.metaera.app.LocalConfigStore;
import com.metaera.app.ui.activities.LoginActivity;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;

public class LoginItemChildClickListener implements OnItemChildClickListener,View.OnClickListener {

    private int[] mViewIDs;

    private OnItemChildClickListener mOnItemChildClickListener;

    private View.OnClickListener mOnClickListener;

    public LoginItemChildClickListener(View.OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public LoginItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        this.mOnItemChildClickListener = onItemChildClickListener;
    }

    public LoginItemChildClickListener(OnItemChildClickListener onItemChildClickListener, @IdRes int ...viewIDs) {
        this.mViewIDs = viewIDs;
        this.mOnItemChildClickListener = onItemChildClickListener;
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View v, int position) {
        if (mViewIDs != null && mViewIDs.length > 0){
            for (int viewID : mViewIDs) {
                if (v.getId() == viewID) {
                    mOnItemChildClickListener.onItemChildClick(adapter, v, position);
                    return;
                }
            }
        }

        if (LocalConfigStore.getInstance().isLogin()) {
            mOnItemChildClickListener.onItemChildClick(adapter, v, position);

        } else {
            v.getContext().startActivity(new Intent(v.getContext(), LoginActivity.class));
        }
    }

    @Override
    public void onClick(View v) {
        if (LocalConfigStore.getInstance().isLogin()) {
            mOnClickListener.onClick(v);
        } else {
            v.getContext().startActivity(new Intent(v.getContext(), LoginActivity.class));
        }
    }

}
